from django.test import TestCase
from question.models import Question, Answer, Correct, Wrong

class QuestionTest(TestCase):

    def setUp(self):
        """
        Setup objects for testing
        """
        self.question = Question.objects.create(
                question='?', 
                question_type='single_select',
        )

        self.answer1 = Answer.objects.create()
        self.answer2 = Answer.objects.create()
        self.answer3 = Answer.objects.create()

        self.correct = Correct.objects.create(question=self.question,
                answer=self.answer1)
        self.wrong1 = Wrong.objects.create(question=self.question, 
                answer=self.answer2)
        self.wrong2 = Wrong.objects.create(question=self.question, 
                answer=self.answer3)

    def test_question_check_answer(self):
        """
        Check that check_answer correctly corrects the given answer.
        """
        self.assertEqual(self.question.check_answer(self.answer1.id), True)
        self.assertEqual(
                self.question.check_answer(str(self.answer1.id)), True)
        self.assertEqual(self.question.check_answer(self.answer2.id), False)
        self.assertEqual(self.question.check_answer(self.answer3.id), False)
        self.assertEqual(
                self.question.check_answer(str(self.answer3.id)), False)
        self.assertEqual(self.question.check_answer(12345), False)
        # empty string is always incorrect
        self.assertEqual(self.question.check_answer(''), False)
        # None is always incorrect
        self.assertEqual(self.question.check_answer(None), False)

    def test_check_fill_in_the_blanks_answer(self):
        """
        Check that check_answer works for fill in the blanks questions.
        """
        question = Question.objects.create(
                question='?', 
                question_type='text_field',
                is_case_sensitive=True,
        )
        answer = Answer.objects.create(answer='Correct')
        correct = Correct.objects.create(question=question, answer=answer)

        self.assertEqual(question.check_answer('Correct'), True)
        # Unicode strings work
        self.assertEqual(question.check_answer(unicode('Correct')), True)
        # Case sensitive
        self.assertEqual(question.check_answer('correct'), False)
        # Wrong answer should return false
        self.assertEqual(question.check_answer('wrong'), False)

    def test_check_case_sensitive_question(self):
        """
        Verify that the check_answer method works for case sensitive questions.
        """
        question = Question.objects.create(
            question='?',
            question_type='text_field',
            is_case_sensitive=True,
        )
        answer = Answer.objects.create(answer='Case Sensitive')
        correct = Correct.objects.create(question=question, answer=answer)

        self.assertEqual(question.check_answer('Case Sensitive'), True)
        self.assertEqual(question.check_answer('case sensitive'), False)


    # TODO: Add test for creating new questions (creation, validation, etc)
    # TODO: Add test for creating new difficulties (creation, validation, etc)

    def test_question_delete(self):
        """
        Check that deleting a question properly deletes it and the attatched
        answers
        """
        # Create question and answers
        question = Question.objects.create(question="?")
        
        answer1 = Answer.objects.create()
        answer2 = Answer.objects.create()
        answer3 = Answer.objects.create()

        correct = Correct.objects.create(question=question, answer=answer1)
        wrong1 = Wrong.objects.create(question=question, answer=answer2)
        wrong2 = Wrong.objects.create(question=question, answer=answer3)

        # Delete question via view
        # TODO: Fix failure because login is required
        response = self.client.get('/questions/delete/' + 
                str(question.id) + '/')

        self.assertEqual(response.status_code, 302)

        # Make sure question and answers are now deleted from database
        self.assertRaises(Question.DoesNotExist, 
                Question.objects.get, pk=question.id)

        self.assertRaises(Correct.DoesNotExist, 
                Correct.objects.get, pk=correct.id)
        self.assertRaises(Wrong.DoesNotExist, 
                Wrong.objects.get, pk=wrong1.id)
        self.assertRaises(Wrong.DoesNotExist, 
                Wrong.objects.get, pk=wrong2.id)

        self.assertRaises(Answer.DoesNotExist, 
                Answer.objects.get, pk=answer1.id)
        self.assertRaises(Answer.DoesNotExist, 
                Answer.objects.get, pk=answer2.id)
        self.assertRaises(Answer.DoesNotExist, 
                Answer.objects.get, pk=answer3.id)

